package com.zsguan.s.stools4andr;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chshqiang on 15-9-3.
 * 文本处理工具
 */
public class STextTool {
    public static boolean isEmpty(CharSequence... args) {
        for (CharSequence str : args) {
            if (str != null && str.length() > 0)
                return false;
        }

        return true;
    }

    public static boolean hasEmpty(CharSequence... args) {
        for (CharSequence str : args) {
            if (str == null || str.length() == 0)
                return true;
        }

        return false;
    }

    public static boolean isEmail(String email) {
        return !isEmpty(email) && email.matches("^[a-z'0-9]+([._-][a-z'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$");
    }

    public static String md5(String str) {
        try {
            byte[] buf = str.getBytes();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(buf);
            byte[] tmp = md5.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : tmp) {
                sb.append(Integer.toHexString(b & 0xff));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return str;
    }


    // 校验Tag Alias 只能是数字,英文字母和中文
    public static boolean isValidTagAndAlias(String s) {
        Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_-]{0,}$");
        Matcher m = p.matcher(s);
        return m.matches();
    }
    public static boolean isChina(String str) {
        String regexStr = "[\u4E00-\u9FA5]";

        for (int i = 0; i < str.length(); i++) {
            String ss = str.substring(i, i + 1);
            if (Pattern.matches(regexStr, ss)) {
                return true;
            }
        }

        return false;
    }

    public static boolean isEnglish(String s){
        return !isEmpty(s)&&s.matches("^[a-zA-Z]{3,200}$");
    }

}
